package com.example.producerservice;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @Classname ProducerServiceImpl
 * @Description
 * @Author caozhenhao001
 * @Date 2024/3/25
 * @Version
 */
@Service
public class ProducerServiceImpl {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public String sendMsg(String msg) {
        String msgId = UUID.randomUUID().toString();
        String sendTime = String.valueOf(System.currentTimeMillis());
        Message message = new Message(msgId, sendTime, msg);
        try {
            rabbitTemplate.convertAndSend(RabbitConstant.RABBITMQ_EXCHANGE, RabbitConstant.RABBITMQ_ROUTING, message.toString());
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }

    }
}
